package com.yoomiito.app.ui.my.pick;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.yoomiito.app.Constant;
import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseActPresenter;
import com.yoomiito.app.model.bean.AddressInfo;
import com.yoomiito.app.model.bean.PickGoods;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.order.list.OrderActivity;
import com.yoomiito.app.utils.NetworkUtils;
import com.yoomiito.app.utils.YMToast;

import java.util.LinkedHashMap;
import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.router.Router;
import okhttp3.RequestBody;

public class PickGoodsPresenter extends BaseActPresenter<PickGoodsActivity> {

    public PickGoodsPresenter(AppConfig appConfig) {
        super(appConfig);
    }


    public void getAddress() {
        YmApi.getYmService()
                .getAddressList(1)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<List<AddressInfo>>() {
                    @Override
                    public void onSuccess(List<AddressInfo> addressInfo) {
                        if (addressInfo.size() == 0) {
                            getV().showAddressView(new AddressInfo());
                        } else {
                            int j = 0;
                            for (int i = 0; i < addressInfo.size(); i++) {
                                if (TextUtils.equals(addressInfo.get(i).getIsDefault(), "1")) {
                                    j = i;
                                }
                            }
                            getV().showAddressView(addressInfo.get(j));
                        }
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                        if (!TextUtils.isEmpty(error.getMsg())) {
                            YMToast.showText(error.getMsg());
                        }
                    }
                });
    }


    public void confirmOrder() {
        YmApi.getYmService()
                .confirmOrder(1)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<PickGoods>() {
                    @Override
                    public void onSuccess(PickGoods addressInfo) {

                        getV().setData(addressInfo.list);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                        if (!TextUtils.isEmpty(error.getMsg())) {
                            YMToast.showText(error.getMsg());
                        }
                    }
                });
    }

    public void createOrder(String goodsList, String consignee, String provinceId,
                            String cityId, String countryId, String tel, String address,
                            String note) {
        if (NetworkUtils.checkNetPermission(getV())) {
            return;
        }
        LinkedHashMap<String, Object> params = new LinkedHashMap<>();
        params.put("province", provinceId);
        params.put("city", cityId);
        params.put("county", countryId);
        params.put("address", address);
        params.put("consignee", consignee);
        params.put("mobile", tel);
        if (!TextUtils.isEmpty(note)) {
            params.put("note", note);
        }
        params.put("goodsList", goodsList);
        RequestBody body = RequestBody.create(YmApi.JSON, JSONObject.toJSONString(params));
        YmApi.getYmService()
                .cOrder(body)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        YMToast.showText("您已提货成功，等待发货");
                        Router.newIntent(getV())
                                .to(OrderActivity.class)
                                .putInt("select", 2)
                                .putInt(Constant.TYPE,1)
                                .launch();
                        getV().finish();
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

}
